package com.slz3.business.module.messageManagement.mesLog.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.messageManagement.mesLog.domain.MesLog;
import com.slz3.business.module.messageManagement.mesLog.domain.MesLogFactory;
import com.slz3.business.module.messageManagement.mesLog.steward.MesLogSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Slf4j
@Service
public class MesLogServiceImpl implements MesLogService {
    @Override
    public MesLog save(MesLog mesLog) {
        return Repositorys.mesLog.save(MesLogFactory.initBaseFields(mesLog));
    }

    @Override
    public Page<MesLog> page(MultiValueMap<String, String> params) {
        Page<MesLog> mesLogPage = Repositorys.mesLog.findAll(
                MesLogSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return mesLogPage;
    }


    @Override
    public MesLog modify(MesLog mesLogNew) {
        return Repositorys.mesLog.saveAndFlush(MesLogFactory.initBaseFields(mesLogNew));
    }

    @Override
    public Optional<MesLog> single(UUID uuid) {
        return Repositorys.mesLog.findById(uuid);
    }

    @Override
    public List<MesLog> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.mesLog.findAllById(uuids);
    }

    @Override
    public List<MesLog> delete(List<MesLog> mesLogList) {
        mesLogList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            MesLogFactory.initBaseFields(item);
        });
        return Services.mesLogTransactional.batchModify(mesLogList);
    }

}
